package com.kin.rich.service.run;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.kin.rich.pojo.Keyword;
import com.kin.rich.service.BaiduWebSpider;
import com.kin.rich.service.SpiderHelper;

public class SpiderRun {
	
	private SpiderHelper helper;
	private BaiduWebSpider baiduWebSpider;
	
	public void setHelper(SpiderHelper helper) {
		this.helper = helper;
	}

	public void setBaiduWebSpider(BaiduWebSpider baiduWebSpider) {
		this.baiduWebSpider = baiduWebSpider;
	}

	public void runSpiderWithThread(){
		//baiduWebSpider.runSpiderWithThread();
	}
	
	public void runSpiderOneByOne(){
		List<Keyword> list = helper.getValidKeyword();
		System.out.println(list);
		for(int i=0;i<list.size();i++){
			
			Keyword keyword = list.get(i);
			if(keyword.getWordId()==35){
				//BaiduWebSpider spider = new BaiduWebSpider(list.get(i));
				baiduWebSpider.search(keyword);
			}
		}
	}
	
	public static SpiderRun getFromApplicationContext(ApplicationContext ctx) {
		return (SpiderRun) ctx.getBean("SpiderRun");
	}
	
	public static void main(String[] args) {
		ApplicationContext appContext = new FileSystemXmlApplicationContext("/src/applicationContext.xml");
		SpiderRun spider =  SpiderRun.getFromApplicationContext(appContext);
		spider.runSpiderOneByOne();
		//spider.runSpiderWithThread();
	}
}
